import config from '../../../config/index.js'
import api from '../../../config/api.js'
import util from '../../../utils/util.js'
import {
    login,
    home,
} from '../../../services/index.js'
var app = getApp();

Page({
  data: {
    cartGoods: [],
    cartTotal: {
      "goodsCount": 0,
      "goodsAmount": 0.00,
      "checkedGoodsCount": 0,
      "checkedGoodsAmount": 0.00
    },
    isEditCart: false,
    checkedAllStatus: true,
    editCartList: []
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数


  },
  onReady: function () {
    // 页面渲染完成

  },
  onShow: function () {
    // 页面显示
    this.GatShopCart();
    this.setcheckedGoodsAmount()
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  GatShopCart: function () {
    let that = this;
    home.GatShopCart({key:"os9rTs9xy3GpTCTrXlztfdVnBUrM"}).then(function (res) {
      if (res.resultCode == 1) {

        console.log(res.data);
        that.setData({
          cartGoods: res.row.allCarts.selectItems,
          // cartTotal: res.data.cartTotal
        });
      }

      that.setData({
        checkedAllStatus: that.isCheckedAll()
      });
    });
  },
  setcheckedGoodsAmount(){
    let checkedGoodsAmount=0
    for (let v of this.data.cartGoods){
      console.log(v.checked)
      if(v.checked == true){
        console.log(checkedGoodsAmount)
        checkedGoodsAmount+=v.count*v.item.fprice
      }
    }
        console.log(checkedGoodsAmount)
        this.setData({
          'cartTotal.checkedGoodsAmount':checkedGoodsAmount>0?checkedGoodsAmount:0.00
        })

  },
  isCheckedAll: function () {
    //判断购物车商品已全选
    return this.data.cartGoods.every(function (element, index, array) {
      if (element.checked == true) {
        return true;
      } else {
        return false;
      }
    });
  },
  checkedItem: function (event) {
    let itemIndex = event.target.dataset.itemIndex;
    let that = this;
    let tmpCartData = this.data.cartGoods.map(function (element, index, array) {
      if (index == itemIndex){
        element.checked = !element.checked;
      }
      
      return element;
    });
    console.log(tmpCartData)

    that.setData({
      cartGoods: tmpCartData,
      // checkedAllStatus: that.isCheckedAll(),
      // 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
    },()=>{
      this.setcheckedGoodsAmount()
    });

  },
  getCheckedGoodsCount: function(){
    let checkedGoodsCount = 0;
    for (let v of this.data.cartGoods){
    console.log(v);
      if (v.checked === true) {
        checkedGoodsCount += v.number;
      }
    }
    return checkedGoodsCount;
  },
  checkedAll: function () {
    for (let v of this.data.cartGoods){
      v.checked = true
    }

    this.setData({
      cartGoods: this.data.cartGoods,
    },()=>{
      this.setcheckedGoodsAmount()
    });
  },
  editCart: function () {
      this.setData({
        isEditCart: !this.data.isEditCart
      });
      return

  },
  updateCart: function (productId, goodsId, number, id) {
    let that = this;

  },
  cutNumber: function (event) {

    let itemIndex = event.target.dataset.itemIndex;
    let cartItem = this.data.cartGoods[itemIndex];
    let count = (cartItem.count - 1 > 1) ? cartItem.count - 1 : 1;
    cartItem.count = count;
    this.setData({
      cartGoods: this.data.cartGoods
    },()=>{
      this.setcheckedGoodsAmount()
    });
    // this.updateCart(cartItem.product_id, cartItem.goods_id, number, cartItem.id);
  },
  addNumber: function (event) {
    console.log('addNumber')
    let itemIndex = event.target.dataset.itemIndex;
    let cartItem = this.data.cartGoods[itemIndex];
    let count = cartItem.count + 1;
    cartItem.count = count>99?99:count;
    this.setData({
      cartGoods: this.data.cartGoods
    },()=>{
      this.setcheckedGoodsAmount()
    });
    // this.updateCart(cartItem.product_id, cartItem.goods_id, number, cartItem.id);

  },
  setcheckbox(e){
    let itemIndex = event.target.dataset.itemIndex;
    let cartItem = this.data.cartGoods[itemIndex];
    cartItem.checked = !cartItem.checked;
    this.setData({
      cartGoods: this.data.cartGoods
    },()=>{
      console.log(this.data.cartGoods)
      this.setcheckedGoodsAmount()
    });
    e.detail.value
  },
  checkoutOrder: function () {
    //获取已选择的商品
    let that = this;

    var checkedGoods = this.data.cartGoods.filter(function (element, index, array) {
      if (element.checked == true) {
        return true;
      } else {
        return false;
      }
    });

    if (checkedGoods.length <= 0) {
      return false;
    }
  },
  deleteCart: function () {
    let cartGoods=[]
    for (let v of this.data.cartGoods){
      if(v.checked == false){
        cartGoods.push(v)
      }
    }

    this.setData({
      cartGoods: cartGoods,
    },()=>{
      this.setcheckedGoodsAmount()
    });
  }
})